Telegram Image Generator Bot

Create custom images directly from Telegram with our bot

ImageGenBot

@ImageGeneratorBot

How to use:

  1. Start a chat with @ImageGeneratorBot
  2. Send /start to begin
  3. Use commands to generate images
  4. Download or share your creations
Open in Telegram

Available Commands

/create [text]

Generate an image with your text on a colored background

/meme [top text]|[bottom text]

Create a meme with top and bottom text

/quote [your quote]

Generate an inspirational quote image

/color [hex color] [text]

Create image with custom background color

Example Output

Generated image preview

PHP Backend Implementation


<?php
// Set your Telegram bot token
$telegramBotToken = 'YOUR_BOT_TOKEN';

// Get the incoming update from Telegram
$update = json_decode(file_get_contents('php://input'), true);

// Check if this is a valid message
if (isset($update['message'])) {
    $chatId = $update['message']['chat']['id'];
    $messageText = $update['message']['text'];
    
    // Handle commands
    if (strpos($messageText, '/start') === 0) {
        sendMessage($chatId, "Welcome to Image Generator Bot! Use /create, /meme, /quote or /color commands to generate images.");
    }
    elseif (strpos($messageText, '/create') === 0) {
        $text = trim(substr($messageText, 7));
        if (empty($text)) {
            sendMessage($chatId, "Please provide text after /create command");
        } else {
            $imageUrl = generateTextImage($text);
            sendPhoto($chatId, $imageUrl);
        }
    }
    elseif (strpos($messageText, '/meme') === 0) {
        $parts = explode('|', trim(substr($messageText, 5)));
        if (count($parts) < 2) {
            sendMessage($chatId, "Please provide top and bottom text separated by |");
        } else {
            $imageUrl = generateMemeImage($parts[0], $parts[1]);
            sendPhoto($chatId, $imageUrl);
        }
    }
    // Add other command handlers here...
}

// Function to generate text image
function generateTextImage($text) {
    // Create a blank image
    $image = imagecreatetruecolor(800, 400);
    
    // Allocate colors
    $bgColor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
    $textColor = imagecolorallocate($image, 255, 255, 255);
    
    // Fill background
    imagefilledrectangle($image, 0, 0, 800, 400, $bgColor);
    
    // Add text
    $font = 'arial.ttf'; // Make sure to have the font file
    imagettftext($image, 36, 0, 50, 200, $textColor, $font, $text);
    
    // Save image to temporary file
    $filename = tempnam(sys_get_temp_dir(), 'telegram_img') . '.png';
    imagepng($image, $filename);
    imagedestroy($image);
    
    return $filename;
}

// Function to send message
function sendMessage($chatId, $text) {
    global $telegramBotToken;
    $url = "https://api.telegram.org/bot$telegramBotToken/sendMessage";
    $data = [
        'chat_id' => $chatId,
        'text' => $text
    ];
    file_get_contents($url . '?' . http_build_query($data));
}

// Function to send photo
function sendPhoto($chatId, $photoPath) {
    global $telegramBotToken;
    $url = "https://api.telegram.org/bot$telegramBotToken/sendPhoto";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    $photo = new CURLFile($photoPath);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'chat_id' => $chatId,
        'photo' => $photo
    ]);
    
    curl_exec($ch);
    curl_close($ch);
    
    // Clean up
    unlink($photoPath);
}
?>
                

How to Set Up:

  1. Create a new bot with @BotFather on Telegram
  2. Get your bot token
  3. Upload this PHP script to your web server
  4. Set the webhook URL in Telegram API: https://api.telegram.org/botYOUR_TOKEN/setWebhook?url=https://yourdomain.com/yourscript.php
  5. Start chatting with your bot!

Requirements:

  • PHP 7.0 or higher
  • GD library installed for image generation
  • SSL certificate (HTTPS) for webhook
  • Write permissions in temporary directory